package com.sanchunhui.psc.ui.adapter

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.content.Intent
import android.view.*
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.eightbitlab.rxbus.Bus
import com.facebook.drawee.view.SimpleDraweeView
import com.noober.menu.FloatMenu
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseConstant
import com.sanchunhui.psc.data.protocol.Team
import com.sanchunhui.psc.event.TeamAllCheckedEvent
import com.sanchunhui.psc.ext.setVisible
import com.sanchunhui.psc.ui.activity.TeamMembersActivity
import com.sanchunhui.psc.ui.fragment.ProjectTeamsFragment
import com.sanchunhui.psc.utils.GlideUtils
import kotlinx.android.synthetic.main.item_project_teams.view.*
import org.jetbrains.anko.find

/**
 * @Author zyx
 * @Date 2021/4/20-10:18
 * @Dscribe 分组列表数据适配器
 */
class ProjectTeamsAdapter(val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val STATE_DEFAULT = 0//默认状态
    internal var mEditMode = STATE_DEFAULT
    lateinit var mListener: ProjectTeamsFragment.OnListFragmentInteractionListener

    var mData: ArrayList<Team> = ArrayList()
    fun setData(data: ArrayList<Team>) {
        mData = data
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        // val itemView = View.inflate(context, R.layout.item_order_item, null)
        //TODO:没有填充满,原因是recycleview的孩子，测量模式是UNSPECIFY
        //通过返回值已经addview,如果attachToRoot使用true会再一次addView()，就会报错
        val view = LayoutInflater.from(context).inflate(R.layout.item_project_teams, parent, false)
        return TeamsListItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as TeamsListItemViewHolder).bindData(mData[position])
    }

    inner class TeamsListItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val sdv_teamIcon: SimpleDraweeView
        val tv_teamName: TextView
        val tv_memberCount: TextView
        val ck_mCheckedCb: CheckBox
        val tv_teamStatusStart: TextView
        val tv_teamStatusStop: TextView
        val tv_teamStatusComplete: TextView
        val tv_teamStatusReady: TextView
        val tv_teamStatusPause: TextView
        lateinit var mTeam: Team

        init {
            sdv_teamIcon = itemView.find(R.id.sdv_teamIcon)
            tv_teamName = itemView.find(R.id.tv_teamName)
            tv_memberCount = itemView.find(R.id.tv_memberCount)
            ck_mCheckedCb = itemView.find(R.id.mCheckedCb)
            tv_teamStatusStart = itemView.find(R.id.tv_teamStatusStart)
            tv_teamStatusStop = itemView.find(R.id.tv_teamStatusStop)
            tv_teamStatusComplete = itemView.find(R.id.tv_teamStatusComplete)
            tv_teamStatusReady = itemView.find(R.id.tv_teamStatusReady)
            tv_teamStatusPause = itemView.find(R.id.tv_teamStatusPause)
            // 点击
            itemView.setOnClickListener {
                val intent: Intent = Intent(context, TeamMembersActivity::class.java)
                intent.putExtra("teamInfo", mTeam)
                context!!.startActivity(intent)
            }
            itemView.mCheckedCb.setOnClickListener {
                mTeam.isSelected = itemView.mCheckedCb.isChecked
                val isAllChecked = mData.all { it.isSelected }
                Bus.send(TeamAllCheckedEvent(isAllChecked))
                notifyDataSetChanged()
            }

            // 长按
            itemView.setOnLongClickListener {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(mTeam)
                }
                true
            }
        }

        @SuppressLint("ResourceAsColor")
        fun bindData(team: Team) {
            this.mTeam = team
            tv_teamName.text = mTeam.name
            tv_memberCount.text = "成员" + mTeam.member_cnt.toString() + "人"
            if (mEditMode == STATE_DEFAULT) {
                ck_mCheckedCb.setVisible(false)
            } else {
                ck_mCheckedCb.setVisible(true)
                ck_mCheckedCb.isChecked = mTeam.isSelected
            }
            if (mTeam.status.equals("start")) {
                tv_teamStatusStart.visibility = View.VISIBLE
                tv_teamStatusStop.visibility = View.GONE
                tv_teamStatusComplete.visibility = View.GONE
                tv_teamStatusReady.visibility = View.GONE
                tv_teamStatusPause.visibility = View.GONE
            } else if (mTeam.status.equals("stop")) {
                tv_teamStatusStart.visibility = View.GONE
                tv_teamStatusStop.visibility = View.VISIBLE
                tv_teamStatusComplete.visibility = View.GONE
                tv_teamStatusReady.visibility = View.GONE
                tv_teamStatusPause.visibility = View.GONE
            } else if (mTeam.status.equals("complete")) {
                tv_teamStatusStart.visibility = View.GONE
                tv_teamStatusStop.visibility = View.GONE
                tv_teamStatusComplete.visibility = View.VISIBLE
                tv_teamStatusReady.visibility = View.GONE
                tv_teamStatusPause.visibility = View.GONE
            } else if (mTeam.status.equals("ready")) {
                tv_teamStatusStart.visibility = View.GONE
                tv_teamStatusStop.visibility = View.GONE
                tv_teamStatusComplete.visibility = View.GONE
                tv_teamStatusReady.visibility = View.VISIBLE
                tv_teamStatusPause.visibility = View.GONE
            } else if (mTeam.status.equals("pause")) {
                tv_teamStatusStart.visibility = View.GONE
                tv_teamStatusStop.visibility = View.GONE
                tv_teamStatusComplete.visibility = View.GONE
                tv_teamStatusReady.visibility = View.GONE
                tv_teamStatusPause.visibility = View.VISIBLE

                //TODO:赋值其他字段
//            Picasso.with(context).load(BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${mTeam.id%1000}/${mTeam.id}.jpg").into(iv_avatar)
                GlideUtils.loadUrlImage(
                    context!!,
                    BaseConstant.IMAGE_SERVER_ADDRESS_URL + "${mTeam.id % 1000}/${mTeam.id}.jpg",
                    sdv_teamIcon
                )
            }
        }
    }

    /**
     * 设置编辑状态   接收Activity中传递的值，并改变Adapter的状态
     */
    fun setEditMode(editMode: Int) {
        mEditMode = editMode
        notifyDataSetChanged()//刷新
    }

}